package com.diaohw.module.websocket.config;


import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static com.diaohw.module.websocket.properties.WsMessageProperties.RABBIT_CONFIG;


@Slf4j
@Configuration
@ConditionalOnProperty(RABBIT_CONFIG)
public class RabbitMQConfig {


    @Bean
    public RabbitTemplate rabbitTemplate(CachingConnectionFactory factory) {

        RabbitTemplate rabbitTemplate = new RabbitTemplate();
        factory.setPublisherConfirmType(CachingConnectionFactory.ConfirmType.CORRELATED);

        factory.setPublisherReturns(true);

        rabbitTemplate.setMandatory(true);

        Jackson2JsonMessageConverter messageConverter = new Jackson2JsonMessageConverter();
        rabbitTemplate.setMessageConverter(messageConverter);
        rabbitTemplate.setReturnsCallback((returnedMessage -> {
            log.info("消息确认");
        }));

        return rabbitTemplate;
    }
}
